#include <stdio.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include <time.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "showGameState.h"
#include "globals.h"

void shuffleDiscards(int player, struct gameState *state)
{
    // Returns all cards in players discard pile to deck and shuffles deck
    int deckindex = state->deckCount[player];
    for (int i = 0; i < state->discardCount[player]; i++)
    {
        state->deck[player][deckindex + i] = state->discard[player][i];
        state->discard[player][i] = 0;
        state->deckCount[player]++;
    }
    state->discardCount[player] = 0;
    gsl_ran_shuffle(rng, state->deck[player], state->deckCount[player], sizeof(enum CARD));
}

void discardTopCard(int player, struct gameState *state)
{
    // Discards the top card from players deck
    state->discard[player][state->discardCount[player]] = state->deck[player][state->deckCount[player]-1];
    state->deckCount[player]--;
    state->discardCount[player]++;
}

void mySeaHagCard(int handpos, struct gameState *state)
{
    // Each other player discards top card of deck
    // and gains a curse card on top of deck
    // If deck is empty, shuffle in discards
    // If still empty, don't discard
    // If not enough curses in supply, deal starting with next player from current player
    // until they run out

    for (int player = 0; player < state->numPlayers; player++)
    {
        if (player != state->whoseTurn)
        {
            if (state->deckCount[player] > 0)
            {
                discardTopCard(player, state);
            }
            else
            {
                shuffleDiscards(player, state);
                if (state->deckCount[player] > 0)
                {
                    discardTopCard(player, state);
                }
            }
            // deal curses
            if (state->supplyCount[curse] > 0) {
            	state->deck[player][state->deckCount[player]] = curse;
            	state->deckCount[player]++;
            	state->supplyCount[curse]--;
            }
        }
    }

}

